from flask import render_template, request,request, jsonify, Blueprint
import requests, socket, jwt, json
import pandas as pd
from .models import *
blue = Blueprint('society', __name__)
static_path='./App/static/'
createFakeUserList = [{
    'userId': '1',
    'username': 'vben',
    'realName': 'Vben Admin',
    'avatar': 'https://q1.qlogo.cn/g?b=qq&nk=190848757&s=640',
    'desc': 'manager',
    'password': '12345678',
    'token': 'fakeToken1',
    'homePath': '/IndexPage/index',
    'roles': [
        {
            'roleName': 'Super Admin',
            'value': 'super',
        },
    ],
},
    {
      'userId': '2',
      'username': 'test',
      'password': '12345678',
      'realName': 'test user',
      'avatar': 'https://q1.qlogo.cn/g?b=qq&nk=339449197&s=640',
      'desc': 'tester',
      'token': 'fakeToken2',
      'homePath': '/dashboard/workbench',
      'roles': [
        {
          'roleName': 'Tester',
          'value': 'test',
        },
      ],
    },
  ]

def find_user_by_token(token):
    userinfo = createFakeUserList
    found_user = None
    for user in userinfo:
        if user['token'] == token:
            found_user = user
            break
    return found_user


@blue.route("/login", methods=['POST'])
def login():
    json_data = request.get_json()  # 获取 JSON 数
    userinfo=createFakeUserList
    found_user=None
    for user in userinfo:
        if user['username'] == json_data['username'] and user['password'] == json_data['password']:
            found_user = user
            break
    result={}
    if found_user!=None:
        keys_to_copy = ['userId', 'token','roles']
        for key in keys_to_copy:
            if key in found_user:
                result[key] = found_user[key]
        data=create_data(result,0)
    else:
        data = create_data(None,-1,'账户密码不对')
    return data

@blue.route("/getUserInfo", methods=["GET"])
def getUserInfo():
    data = request.data
    # print(data)
    tokens=request.headers.get('Authorization')
    found_user=find_user_by_token(tokens)
    result={}
    if found_user!= None:
        keys_to_copy = ['userId','username','realName','avatar','desc',"homePath"]
        for key in keys_to_copy:
            if key in found_user:
                result[key] = found_user[key]
        result.update({'roles':{
                'roleName': 'Super Admin',
                'value': 'super',
            }})
        data=create_data(result)
    else:
        data = create_data(None,-1,'userinfo错了')
    return data


@blue.route("/getPermCode", methods=["GET"])
def getPermCode():
    user_id = -1
    tokens = request.headers.get('Authorization')
    found_user = find_user_by_token(tokens)
    user_id = int(found_user['userId'])
    if user_id==1:
        result= ['1000', '3000', '5000']

    else:
        result = ['2000', '4000', '6000']

    data=create_data(result)
    return data
@blue.route("/logout", methods=["GET", 'POST'])
def logout():
    return {
        'code': 0
    }

@blue.route("/getMenuList", methods=["GET", 'POST'])
def getMenuList():
    # 这个函数是用来路由
    tokens = request.headers.get('Authorization')
    found_user = find_user_by_token(tokens)
    userId=found_user['userId']
    result=[]
    # filenames=["dashboard.json","example.json","forms.json","pdf.json"]
    filenames=["indexPage.json","dashboard.json","forms.json","notice.json"]

    for i in filenames:
        json_file_path="App/static/routes/"+i
        with open(json_file_path, 'r',encoding="utf-8") as json_file:
        # 使用 json.load() 方法加载 JSON 数据
            router=json.loads(json_file.read())
        result.append(router)
    if userId == '2':
# 权限分别
        result[2]["children"]=[result[2]["children"][0],result[2]["children"][1]]
        pass
    # print(result[2]["children"])
    return create_data(result)
